package com.jack.vipmis.vip.business.service;

import java.util.Map;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Service;

import com.google.common.collect.Maps;
import com.jack.vipmis.common.entity.search.SearchRequest;
import com.jack.vipmis.common.entity.search.Searchable;
import com.jack.vipmis.common.service.BaseService;
import com.jack.vipmis.vip.business.entity.Business;
import com.jack.vipmis.vip.business.repository.BusinessRepository;

/**
 * 
 * @author JackLiang
 * @date 2014-6-7
 * @version 1.0
 */
@Service
public class BusinessService extends BaseService<Business, Long> {

	private BusinessRepository getBusinessRepository() {
		return (BusinessRepository) baseRepository;
	}

	/**
	 * 分页查询
	 * 
	 * @param page
	 * @param pageSize
	 * @param sortType
	 * @param type
	 * @param classType
	 * @return
	 */
	public Page<Business> getBusinessByPage(int page, int pageSize, String sortType, String type) {
		Map<String, Object> searchParams = Maps.newHashMap();
		searchParams.put("businessType", type);
		Searchable searchable = buildSearchable(page, pageSize, searchParams, sortType);
		return getBusinessRepository().findAll(searchable);
	}

	/**
	 * 创建动态查询条件组合.
	 */
	private Searchable buildSearchable(int page, int pageSize, Map<String, Object> searchParams, String sortType) {
		Searchable searchRequest = SearchRequest.newSearchable(searchParams);
		searchRequest.addSort(Direction.DESC, sortType);
		searchRequest.setPage((page - 1), pageSize);
		return searchRequest;
	}

	public Business findByBsName(String fieldValue) {
		return getBusinessRepository().findByBsName(fieldValue);
	}
}
